﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace Assist
{
	public class WallAssistFunc
	{
		public static WallDatum GetWallDatum(Autodesk.Revit.DB.Document doc, Wall wall)
		{
			WallDatum wallDatum = new WallDatum();
			Parameter parameter = wall.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM);
			WallType wallType = RevitVersionFuncs.GetElement(doc, parameter.AsElementId()) as WallType;
			wallDatum.wallType = wallType;
			wallDatum.curveType = WallAssistFunc.GetWallCurveType(wall);
			wallDatum.level = WallAssistFunc.GetWallBottomAssoLevel(doc, wall);
			wallDatum.idTopLevel = wall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).AsElementId();
			bool flag = wallType.Kind == 0;
			if (flag)
			{
				parameter = wallType.get_Parameter(BuiltInParameter.WALL_ATTR_WIDTH_PARAM);
				double val = parameter.AsDouble();
				wallDatum.width = AssistFunc.feetToMM(val);
			}
			else
			{
                bool flag2 = (int)wallType.Kind == 2;
				if (flag2)
				{
				}
			}
			wallDatum.ptStart = WallAssistFunc.GetStartPt(wall);
			wallDatum.ptEnd = WallAssistFunc.GetEndPt(wall);
			return wallDatum;
		}

		public static Level GetWallBottomAssoLevel(Autodesk.Revit.DB.Document doc, Wall wall)
		{
			ElementId id = wall.get_Parameter(BuiltInParameter.WALL_BASE_CONSTRAINT).AsElementId();
			return RevitVersionFuncs.GetElement(doc, id) as Level;
		}

		public static IList<FamilyInstance> GetWallAssoDoorWins(Autodesk.Revit.DB.Document doc, Wall wall, BuiltInCategory category)
		{
			Level level = null;
			return WallAssistFunc.GetWallAssoDoorWins(doc, wall, category, level);
		}

		public static IList<FamilyInstance> GetWallAssoDoorWins(Autodesk.Revit.DB.Document doc, Wall wall, BuiltInCategory category, Level level)
		{
			IList<FamilyInstance> list = null;
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(category);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			IList<Element> list2 = filteredElementCollector.OfClass(typeof(FamilyInstance)).WherePasses(elementCategoryFilter).ToElements();
			foreach (Element element in list2)
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				Element host = familyInstance.Host;
				bool flag = host != null;
				if (flag)
				{
					ElementId obj = host.get_Parameter(BuiltInParameter.ID_PARAM).AsElementId();
					ElementId elementId = wall.get_Parameter(BuiltInParameter.ID_PARAM).AsElementId();
					bool flag2 = elementId.Equals(obj);
					if (flag2)
					{
						bool flag3 = list == null;
						if (flag3)
						{
							list = new List<FamilyInstance>();
						}
						bool flag4 = level == null;
						if (flag4)
						{
							list.Add(familyInstance);
						}
						else
						{
							ElementId elementId2 = familyInstance.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM).AsElementId();
							ElementId obj2 = level.get_Parameter(BuiltInParameter.ID_PARAM).AsElementId();
							bool flag5 = elementId2.Equals(obj2);
							if (flag5)
							{
								list.Add(familyInstance);
							}
						}
					}
				}
			}
			return list;
		}

		public static XYZ GetStartPt(Wall wall)
		{
			LocationCurve locationCurve = wall.Location as LocationCurve;
			return locationCurve.Curve.GetEndPoint(0);
		}

		public static bool SetStartPt(Wall wall, XYZ pt)
		{
			LocationCurve locationCurve = wall.Location as LocationCurve;
			return true;
		}

		public static XYZ GetEndPt(Wall wall)
		{
			LocationCurve locationCurve = wall.Location as LocationCurve;
			return locationCurve.Curve.GetEndPoint(1);
		}

		public static WallCurveType GetWallCurveType(Wall wall)
		{
			WallCurveType result = WallCurveType.eError;
			LocationCurve locationCurve = wall.Location as LocationCurve;
			Line line = locationCurve.Curve as Line;
			bool flag = line != null;
			if (flag)
			{
				result = WallCurveType.eLineWall;
			}
			Arc arc = locationCurve.Curve as Arc;
			bool flag2 = arc != null;
			if (flag2)
			{
				result = WallCurveType.eArcWall;
			}
			return result;
		}

		public static Curve GetWallCurve(Wall wall)
		{
			LocationCurve locationCurve = wall.Location as LocationCurve;
			return locationCurve.Curve;
		}

		public static double GetWallThick(Autodesk.Revit.DB.Document doc, Wall wall)
		{
			Parameter parameter = wall.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM);
			WallType wallType = RevitVersionFuncs.GetElement(doc, parameter.AsElementId()) as WallType;
			parameter = wallType.get_Parameter(BuiltInParameter.WALL_ATTR_WIDTH_PARAM);
			bool flag = parameter == null;
			double result;
			if (flag)
			{
				result = 0.0;
			}
			else
			{
				double val = parameter.AsDouble();
				result = AssistFunc.feetToMM(val);
			}
			return result;
		}

		public static bool ResetLineWall(Autodesk.Revit.ApplicationServices.Application app, Autodesk.Revit.DB.Document doc, Wall wall, XYZ ptStart, XYZ ptEnd)
		{
			Line curve = app.CreatYJKLine(ptStart, ptEnd, true);
			LocationCurve locationCurve = wall.Location as LocationCurve;
			locationCurve.Curve = curve;
			return true;
		}

		private const double precision = 1E-05;
	}
}
